/*
 * GYPackage.cpp
 *
 *  Created on: 2015年9月23日
 *      Author: gaoyi
 */

#include "GYPackage.h"
#include "CTMsgBase.h"


int GYDecodePBPackage::Init()
{
	m_stBuffer.Attach(NULL, CTMsgHead::EMAX_MSGLEN + sizeof(CEnpNetHead), 0);
	return 0;
}

int GYDecodePBPackage::DecodeEnpMsg()
{
	if (m_stBuffer.GetUsedLen() < (int)sizeof(CEnpNetHead))
	{
        CT_ERROR(("AttachProtoBufMsg failed! buffer size < (int)sizeof(CEnpNetHead)",m_stBuffer.GetUsedLen()));
        return 1;
	}

    //先获取NetHead
	m_stEnpHead = *(CEnpNetHead*)m_stBuffer.GetUsedBuf();
	int iRet = 0;

	if (m_stBuffer.GetUsedLen() != (int)sizeof(CEnpNetHead)+m_stEnpHead.unDataLength)
	{
        CT_ERROR(("DecodeEnpMsg failed! iSize length invalid. %d != %d + %d", m_stBuffer.GetUsedLen(), sizeof(CEnpNetHead), m_stEnpHead.unDataLength));
        return 1;
	}

	if (m_stEnpHead.unDataLength <= sizeof(int))
	{
		CT_INFO(("ecodeEnpMsg failed! m_stHead.unDataLength <= sizeof(int)"));
		return 2;
	}

	if (m_stEnpHead.ucDeodeMethod != CODE_PROTOBUF)
	{
		return 3;
	}

    //再获取MsgHead
    int iMsgHeadLen = ntohl(*(int*)(m_stBuffer.GetUsedBuf() + sizeof(CEnpNetHead)));
    if (iMsgHeadLen <= 0)
    {
        CT_ERROR(("msghead buff is empty(%d)!", iMsgHeadLen));
        return 1;
    }

    if (iMsgHeadLen > m_stEnpHead.unDataLength - (int)sizeof(int))
    {
        CT_ERROR(("msghead buff length (%d) bigger than data length(%d)!", iMsgHeadLen, m_stEnpHead.unDataLength));
        return 1;
    }

    iRet = DecodePBMsg(m_stMsgHead, (void *)(m_stBuffer.GetUsedBuf() + sizeof(CEnpNetHead)+sizeof(int)), iMsgHeadLen);
    if (iRet)
    {
    	CT_ERROR(("DecodePBMsg failed!"));
    	return 1;
    }

    CT_TRACE(("version=%d time=%d msgact=%d msgtype=%d msgid=%d",
    		m_stMsgHead.version(),m_stMsgHead.time(),m_stMsgHead.msgact(), m_stMsgHead.msgtype(),m_stMsgHead.msgid()));

    //再处理MsgBody
    if (m_stEnpHead.unDataLength <= sizeof(int) + iMsgHeadLen + sizeof(int))
    {
        CT_ERROR(("MsgHeadLen %d Head Data Length %d", sizeof(int) + iMsgHeadLen + sizeof(int),
        		m_stEnpHead.unDataLength));
        return 1;
    }

    int iMsgBodyLen = ntohl(*(int*)(m_stBuffer.GetUsedBuf() + sizeof(CEnpNetHead) + sizeof(int) + iMsgHeadLen));
    if (iMsgBodyLen <= 0)
    {
        CT_ERROR(("msgbody buff is empty(%d)!", iMsgBodyLen));
        return 1;
    }

    CT_TRACE(("iMsgBodyLen = %d", iMsgBodyLen));

    if (iMsgBodyLen > m_stEnpHead.unDataLength - (int)sizeof(int) - iMsgHeadLen - (int)sizeof(int))
    {
        CT_ERROR(("msgbody buff length (%d) bigger than data length(%d)!", iMsgBodyLen,
        		m_stEnpHead.unDataLength - (int)sizeof(int) - iMsgHeadLen - (int)sizeof(int)));
        return 1;
    }

    //这里都是处理来自client的请求
    iRet = m_stCodeStream.Attach(m_stBuffer.GetUsedBuf() + sizeof(CEnpNetHead) + sizeof(int) + iMsgHeadLen + sizeof(int),
        iMsgBodyLen);
    CHECK_THROW(iRet);

	return 0;
}

int	GYDecodePBPackage::DecodePBMsg(::google::protobuf::Message& stMsgBody, void* pData, int iLength)
{
    CT_ASSERT(pData);

    if (!stMsgBody.ParseFromArray((void*)pData, iLength))
    {
        CT_ERROR(("%s %s", stMsgBody.GetTypeName().c_str(), stMsgBody.InitializationErrorString().c_str()));
        return 1;
    }

    return 0;
}


